/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seso;

import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;


/**
 *
 * @author Administrator
 */
public class Util {

    public static String getClassJarPath() {
        String classPath = (Util.class.getResource("")).toString();
        String resultPath = "";

        if (classPath.substring(0, 6).compareTo("file:/") == 0) {
            resultPath = classPath.substring(5);
        } else {
            int index = classPath.indexOf(".jar!/");
            if (index > 9) {
                resultPath = classPath.substring(9, classPath.indexOf(".jar!/"));
            }

            index = resultPath.lastIndexOf("/");
            if (index > 0) {
                resultPath = resultPath.substring(0, index) + "/";
            }
        }

        return resultPath;
    }

    public static ImageIcon getUserImageIcon(String relativeURL) {
        String leadingPath = "resources/";
        if (! relativeURL.substring(0, leadingPath.length()).toLowerCase().equals(leadingPath)){
            relativeURL = leadingPath + relativeURL;
        }
        return (new ImageIcon(Util.getClassJarPath() + relativeURL));
    }

    public static Image getUserImage(String relativeURL) {
        String leadingPath = "resources/";
        if (! relativeURL.substring(0, leadingPath.length()).toLowerCase().equals(leadingPath)){
            relativeURL = leadingPath + relativeURL;
        }
        return getUserImageIcon(relativeURL).getImage();
    }

    public static Image getLogoImage() {
        return getUserImage("resources/logo.png");
    }
    
    
    public static void runExe(String cmd) {
        runExe(cmd, 0);
    }

    public static void runExe(String cmd, int sleep) {
        Runtime rn = Runtime.getRuntime();
        Process p = null;
        try {
            p = rn.exec(cmd);
            Thread.sleep(sleep);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error exec: " + e.getMessage());
        }
    }    
}
